package com.zerone.pay.trade.web.controller;

import com.acooly.core.utils.Ids;
import com.github.wxpay.sdk.WXPayConstants;
import com.github.wxpay.sdk.WXPayUtil;
import com.google.common.collect.Maps;
import com.zerone.pay.common.PayClient;
import com.zerone.pay.common.TradeConstants;
import com.zerone.pay.gateway.WXPayConfigMore;
import com.zerone.pay.trade.enums.PayMethodEnum;
import com.zerone.pay.trade.service.TradeService;
import com.zerone.pay.trade.service.order.TradeCreateOrder;
import com.zerone.pay.trade.web.Pay;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author 木鱼 muyu@yiji.com
 */
@Controller
@RequestMapping({"/pay/"})
public class PayController {


    @Autowired
    private TradeService tradeService;

    @Autowired
    private WXPayConfigMore wxPayConfigMore;

    @RequestMapping("/key")
    @ResponseBody
    public String key(@RequestParam("paramUrl") String paramUrl) {
        return PayClient.sign(TradeConstants.KEY,paramUrl);
    }

    @RequestMapping("/new")
    public String newPay() {
        return "pay";
    }

    @RequestMapping("/genQR")
    public String genQR() {
        return "genQR";
    }

    @RequestMapping("/alipay")
    public String alipay(@ModelAttribute Pay pay, Model model) throws Exception {
        TradeCreateOrder order = new TradeCreateOrder();
        order.setPartnerId(pay.getPartnerId());
        order.setAmount(pay.getAmount());
        order.setOutOrderNo(Ids.getDid());
        order.setGid(Ids.gid());
        order.setPayMethod(PayMethodEnum.ali_code.code());
        order.setMemo("支付宝支付");
        order.setUserId(pay.getUserId());
        String tradeNO = tradeService.create(order);
        model.addAttribute("tradeNO",tradeNO);
        return "alipayCall";
    }

    @RequestMapping("/wxPay")
    public String wxPay(@ModelAttribute Pay pay, Model model, HttpServletRequest request) throws Exception {
        TradeCreateOrder order = new TradeCreateOrder();
        order.setPartnerId(pay.getPartnerId());
        order.setAmount(pay.getAmount());
        order.setOutOrderNo(Ids.getDid());
        order.setGid(Ids.gid());
        order.setPayMethod(PayMethodEnum.wechat_code.code());
        order.setMemo("微信支付");
        order.setUserId(pay.getUserId());
        order.setIp(request.getLocalAddr());
        String tradeNO = tradeService.create(order);
        Map<String,String> param = Maps.newHashMap();
        param.put("appId",wxPayConfigMore.getAppID());
        param.put("timeStamp",String.valueOf(System.currentTimeMillis()));
        param.put("nonceStr",WXPayUtil.generateNonceStr());
        param.put("package","prepay_id="+tradeNO);
        param.put("signType",wxPayConfigMore.getSignType().name());


        String sign =null;
        if(wxPayConfigMore.getSignType() == WXPayConstants.SignType.MD5){
            sign= WXPayUtil.generateSignature(param,wxPayConfigMore.getKey());
        }else{
            sign=WXPayUtil.generateSignature(param,wxPayConfigMore.getKey(),
                    WXPayConstants.SignType.HMACSHA256);
        }
        param.put("paySign",sign);
        model.addAttribute("data",param);
        return "wechatCall";
    }



    @RequestMapping("/cashier")
    public String cashier() {
        return "cashier";
    }

}
